package kcms.common.action;

import java.security.MessageDigest;
import java.util.List;

import javax.annotation.Resource;

import kcms.common.service.CommonService;
import kcms.db.User;
import kcms.db.ViewAssignScore;
import kcms.db.ViewMessage;

import org.springframework.context.annotation.Scope;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@Controller("CommonAction")
@Scope("prototype")
public class CommonAction  extends ActionSupport{
	private static final long serialVersionUID = 1L;
	@Resource
	private CommonService commonservice;
	private User user;
	private List<ViewMessage> vms;
	private List<ViewAssignScore> vas;
	private String token;
	
	
	/*mail*/
	@Resource
	private MailSender mailSender;
	@Resource
    private SimpleMailMessage simpleMailMessage;
	
	
	public String init(){
		setUser((User) ActionContext.getContext().getSession().get("user"));
		//得到老师给学生的教务通知
		setVms(commonservice.getMessagesSize(user.getId()));
		//得到老师给学生的作业评价通知
		setVas(commonservice.getAssignMessagesSize(user.getId()));
		if(user.getRole().equals("student"))
		return "stuinitsuccess";
		else if(user.getRole().equals("teacher"))
			return "teainitsuccess";
		else
			return "logout";
	}
	
	public String logout(){
		ActionContext.getContext().getSession().remove("user");
		return "logout";
	}
	
	public boolean sendMail(String subject,String receiver,String text) {
		try{
		 simpleMailMessage.setSubject(subject); //设置邮件主题
	     simpleMailMessage.setTo(receiver);             //设定收件人
	     simpleMailMessage.setText(text);  //设置邮件主题内容
	     mailSender.send(simpleMailMessage); //发送邮件
	     return true;}
		catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public String sha1(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("SHA1");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];
 
        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
 
        byte[] md5Bytes = md5.digest(byteArray);
 
        StringBuffer hexValue = new StringBuffer();
 
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
 
        return hexValue.toString();
    }
	
	public String verify(){
		if(commonservice.verify(token)){
			
			return "loginpage";
		}
		else {
			return "regpage";
		}
	}
	
	public CommonService getCommonservice() {
		return commonservice;
	}
	public void setCommonservice(CommonService commonservice) {
		this.commonservice = commonservice;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public List<ViewMessage> getVms() {
		return vms;
	}

	public void setVms(List<ViewMessage> vms) {
		this.vms = vms;
	}

	public List<ViewAssignScore> getVas() {
		return vas;
	}

	public void setVas(List<ViewAssignScore> vas) {
		this.vas = vas;
	}

	public MailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public SimpleMailMessage getSimpleMailMessage() {
		return simpleMailMessage;
	}

	public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
		this.simpleMailMessage = simpleMailMessage;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}
}
